/*
 * 2015-10-14 tjg 调派建议 
 * 
 * */

//三种驾车策略：最少时间，最短距离，避开高速
var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME, BMAP_DRIVING_POLICY_LEAST_DISTANCE, BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];
var A2toE1Line;//A2到E1的线路指示
var A2toE2Line;
var A3toE2Line;

//清除建议
function clearSuggestion() {
	
	if (A2toE1Line) {
		map.removeOverlay(A2toE1Line);
		A2toE1Line = null;
	}
	
	if (A2toE2Line) {
		map.removeOverlay(A2toE2Line);
		A2toE2Line = null;
	}
	
	if (A3toE2Line) {
		map.removeOverlay(A3toE2Line);
		A3toE2Line = null;
	}
	
	Gid('routeResult').style.display = "none";
	Gid("routeResult").innerHTML = "";
}

//画推荐路线 type 1:a2->e1, 2:a2->e2, 3:a3->e2  
function drawSuggestionLine(detailID, e_x, e_y, a_x, a_y, type) {
	var rp = routePolicy[0];//默认最少时间
	if (suggestionUseDistance)
		rp = routePolicy[1];//最短距离

	var options = 
	{
		onSearchComplete: function(results)	{
			//alert(ambDriving.getStatus());
			if (driving.getStatus() == BMAP_STATUS_SUCCESS){
				// 获取第一条方案
				var plan = results.getPlan(0);
				var distance = plan.getDistance();
				var duration = plan.getDuration();
				var ptsAtoE = results.getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
				//alert(ptsA1toE1);
				if (type == 1) { //a2->e1
					A2toE1Line = new BMap.Polyline(ptsAtoE, { strokeColor: "blue", strokeWeight: 6, strokeStyle: "solid" });
					map.addOverlay(A2toE1Line); //画线
				}
				else if (type == 2) { //a2->e2
					A2toE2Line = new BMap.Polyline(ptsAtoE, { strokeColor: "brown", strokeWeight: 6, strokeStyle: "dashed" });
					map.addOverlay(A2toE2Line); //画线
				}
				else if (type == 3) { //a2->e2
					A3toE2Line = new BMap.Polyline(ptsAtoE, { strokeColor: "purple", strokeWeight: 6, strokeStyle: "solid" });
					map.addOverlay(A3toE2Line); //画线
				}
				//map.setViewport(ptsAtoE);
				
				//地图回调窗口函数
                window.external.saveAmbSuggestionPath(detailID, JSON.stringify(ptsAtoE), type);
			}
		},
		policy: rp
	};
    var driving = new BMap.DrivingRoute(map, options);  //驾车实例 策略最短时间
    var pte = new BMap.Point(e_x, e_y);//事件
    var pta = new BMap.Point(a_x, a_y);//车辆
    driving.search(pta, pte);
}

//显示待命车辆的调度建议
function showWaitAmbSuggestion(detailID, e1_x, e1_y, a2_x, a2_y, describle) {
	clearSuggestion();
	
	drawSuggestionLine(detailID,e1_x, e1_y, a2_x, a2_y, 1);
	
	map.setViewport([new BMap.Point(e1_x, e1_y), new BMap.Point(a2_x, a2_y)]);
	map.setZoom(map.getViewport().zoom - 1); //减少一级缩放等级
	Gid('routeResult').style.display = "";
	Gid("routeResult").innerHTML = describle;
}

//显示待命车辆的调度建议--历史播放
function showWaitAmbSuggestionHis(path, describle) {
	clearSuggestion();
	
	var ptstmp = JSON.parse(path);
	var length = ptstmp.length;
	var ptsA2toE1= [];
	for (var i=0 ; i < length; i++) {
		var pt = ptstmp[i];
		ptsA2toE1.push(new BMap.Point(pt.lng, pt.lat));
	}
	
	A2toE1Line = new BMap.Polyline(ptsA2toE1, { strokeColor: "blue", strokeWeight: 6, strokeStyle: "solid" });
	map.addOverlay(A2toE1Line); //画线
	map.setViewport(ptsA2toE1);
	Gid('routeResult').style.display = "";
	Gid("routeResult").innerHTML = describle;
}

//显示任务中车辆的改派调度建议
function showTaskAmbSuggestion(detailID, e1_x, e1_y, a2_x, a2_y, e2_x, e2_y, a3_x, a3_y, describle) {
	clearSuggestion();
	
	drawSuggestionLine(detailID,e1_x, e1_y, a2_x, a2_y, 1);
	drawSuggestionLine(detailID,e2_x, e2_y, a2_x, a2_y, 2);
	drawSuggestionLine(detailID,e2_x, e2_y, a3_x, a3_y, 3);
    

    map.setViewport([new BMap.Point(e1_x, e1_y), new BMap.Point(a2_x, a2_y),new BMap.Point(e2_x, e2_y), new BMap.Point(a3_x, a3_y)]);
    map.setZoom(map.getViewport().zoom - 1); //减少一级缩放等级
	Gid('routeResult').style.display = "";
	Gid("routeResult").innerHTML = describle;
}

//显示任务中车辆的改派调度建议--历史播放
function showTaskAmbSuggestionHis(a2toE1Path, a2toE2Path, a3toE2Path, describle) {
	clearSuggestion();
	
	var ptstmp = JSON.parse(a2toE1Path);
	var length = ptstmp.length;
	var ptsA2toE1= [];
	for (var i=0 ; i < length; i++) {
		var pt = ptstmp[i];
		ptsA2toE1.push(new BMap.Point(pt.lng, pt.lat));
	}
	A2toE1Line = new BMap.Polyline(ptsA2toE1, { strokeColor: "blue", strokeWeight: 6, strokeStyle: "solid" });
	map.addOverlay(A2toE1Line); //画线
	
	ptstmp = JSON.parse(a2toE2Path);
	length = ptstmp.length;
	var ptsA2toE2= [];
	for (var i=0 ; i < length; i++) {
		var pt = ptstmp[i];
		ptsA2toE2.push(new BMap.Point(pt.lng, pt.lat));
	}
	A2toE2Line = new BMap.Polyline(ptsA2toE2, { strokeColor: "brown", strokeWeight: 6, strokeStyle: "dashed" });
	map.addOverlay(A2toE2Line); //画线
	
	ptstmp = JSON.parse(a3toE2Path);
	length = ptstmp.length;
	var ptsA3toE2= [];
	for (var i=0 ; i < length; i++) {
		var pt = ptstmp[i];
		ptsA3toE2.push(new BMap.Point(pt.lng, pt.lat));
	}
	A3toE2Line = new BMap.Polyline(ptsA3toE2, { strokeColor: "purple", strokeWeight: 6, strokeStyle: "solid" });
	map.addOverlay(A3toE2Line); //画线
	
	map.setViewport(ptsA2toE1.concat(ptsA2toE2).concat(ptsA3toE2));
	map.setZoom(map.getViewport().zoom - 1); //减少一级缩放等级
	Gid('routeResult').style.display = "";
	Gid("routeResult").innerHTML = describle;
}
